define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking/stock/index' + location.search,
                    add_url: 'clothmaking/stock/add' + location.search,
                    edit_url: 'clothmaking/stock/edit',
                    del_url: 'clothmaking/stock/del',
                    multi_url: 'clothmaking/stock/multi',
                    import_url: 'clothmaking/stock/import',
                    table: 'clothmaking_stock',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function(params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {}; //并将搜索过滤器 转为对象方便我们追加条件
                    filter.product_id = Config.porductid; //将透传的参数 Config.group_id，追加到搜索条件中
                    op.product_id = "="; //group_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'product_id', title: __('Product_id') },
                        {
                            field: 'num',
                            title: __('Num'),
                            formatter(value, row, index) {
                                if (row.productstock_sum === null) {
                                    return value;
                                }
                                return value + row.productstock_sum;
                            }
                        },
                        { field: 'status', title: __('Status'), searchList: { "待审核": __('待审核'), "已审核": __('已审核'), "已取消": __('已取消') }, formatter: Table.api.formatter.status },
                        { field: 'user_id', title: __('User_id') },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            product_id = Config.porductid;
            if (product_id > 0) { $("#c-product_id").data("init", product_id); }
            Controller.api.bindevent();

        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});